<?php
	require_once( '../_config/base.inc.php' );

	use \dayscript\Application;
	use \dayscript\util\Debug;
	use \dayscript\util\Benchmark;
	use \dayscript\util\JSON;
	use \dayscript\ErrorManager;
	use \dayscript\Enviroment;
	
	Application::getInstance( BASE_DIR . '_config/tests.inc.php' );
	ErrorManager::setUproarMode( true ); // Not for production
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<title>Benchmark</title>
	<link rel="stylesheet" type="text/css" media="all" href="../_resources/styles.css" />
</head>
<body>
<b>Benchmark:</b>
<?php
	function show( $name, $from, $to, $color = null )	{
		if( is_numeric( $color ) )	{
			$r = dechex( hexdec( '55' ) + ( hexdec( 'FF' ) - hexdec( '55' ) ) * $color );
			$g = dechex( hexdec( '99' ) - ( hexdec( '99' ) - hexdec( '55' ) ) * $color );
			$color = '#' . $r . $g . '55';
		}
?>
<hr />
<div style="<?php echo ( $color ? 'color:' . $color : '' )?>">
<b><?php echo $name ?>:</b>
<br />
<b>Tiempo:</b>&nbsp;&nbsp;<?php echo Benchmark::getElapsedTime( $from, $to ); ?> seg.
<br />
<b>Memoria:</b>&nbsp;<?php echo Benchmark::getElapsedMemory( $from, $to ); ?> bytes.
</div>
<?php
	} 

	Benchmark::setMark( 'step1' );
	sleep( 1 );
	Benchmark::setMark( 'step2' );
	show( 'Sleep 1 seg.', 'step1', 'step2' );
	
	$total = 2000;
	
	echo "<h2>Crear</h2>";
	
		
	Benchmark::setMark( 'vec1' );
	$vector = array( );
	for( $c = 0; $c < $total; $c ++ )
		$vector[] = $c;
	unset( $vector );
	Benchmark::setMark( 'vec2' );
	show( 'Llenando vector[] = X de ' . $total . ' elementos', 'vec1', 'vec2', 1/4 );
		
	Benchmark::setMark( 'vec3' );
	$vector = array( );
	for( $c = 0; $c < $total; $c ++ )
		$vector[$c] = $c;
	unset( $vector );
	Benchmark::setMark( 'vec4' );
	show( 'Llenando vector[X] = X de ' . $total . ' elementos', 'vec3', 'vec4', 0 );
	
	Benchmark::setMark( 'vec31' );
	$vector = array( );
	for( $c = 0; $c < $total; $c ++ )
		$vector[count($vector) - 1] = $c;
	unset( $vector );
	Benchmark::setMark( 'vec41' );
	show( 'Llenando vector[count($vector) - 1] = X de ' . $total . ' elementos', 'vec31', 'vec41', 1 );
	
	Benchmark::setMark( 'vec5' );
	$vector = array( );
	for( $c = 0; $c < $total; $c ++ )
		\array_push( $vector, $c );
	Benchmark::setMark( 'vec6' );
	show( 'Llenando array_push( vector, X ) de ' . $total . ' elementos', 'vec5', 'vec6', 3/4 );
	
	Benchmark::setMark( 'vec7' );
	$asoc = array( );
	for( $c = 0; $c < $total; $c ++ )
		$asoc['_' . $c] = $c;
	Benchmark::setMark( 'vec8' );
	show( 'Llenando $asoc[\'_\' . X] = X; de ' . $total . ' elementos', 'vec7', 'vec8', 2/4 );

	
	
	
	echo "<h2>Recorrer vector</h2>";
	
	Benchmark::setMark( 'rec1' );
	for( $c = 0; $c < count( $vector ); $c ++ )
		if( $vector[$c])
			;
	Benchmark::setMark( 'rec2' );
	show( 'Recorriendo con FOR/COUNT de ' . $total . ' elementos', 'rec1', 'rec2', 1 );
	
	Benchmark::setMark( 'rec3' );
	for( $c = 0, $max = count( $vector ); $c < $max; $c ++ )
		if( $vector[$c])
			;
	Benchmark::setMark( 'rec4' );
	show( 'Recorriendo con FOR de ' . $total . ' elementos', 'rec3', 'rec4', 0 );
	
	Benchmark::setMark( 'rec5' );
	foreach( $vector as $value )
		if( $value )
			;
	Benchmark::setMark( 'rec6' );
	show( 'Recorriendo con FOREACH => $value de ' . $total . ' elementos', 'rec5', 'rec6', 2/10 );
	
	Benchmark::setMark( 'rec7' );
	foreach( $vector as $key => $value )
		if( $value )
			;
	Benchmark::setMark( 'rec8' );
	show( 'Recorriendo con FOREACH $key => $value de ' . $total . ' elementos', 'rec7', 'rec8', 4/10 );
	
	
	
	echo "<h2>Recorrer vector asociativo</h2>";
	
	Benchmark::setMark( 'aso1' );
	foreach( $asoc as $value )
		if( $value )
			;
	Benchmark::setMark( 'aso2' );
	show( 'Recorriendo ASOC con FOREACH => $value de ' . $total . ' elementos', 'aso1', 'aso2', 0 );
	
	Benchmark::setMark( 'aso3' );
	foreach( $asoc as $key => $value )
		if( $value )
			;
	Benchmark::setMark( 'aso4' );
	show( 'Recorriendo ASOC con FOREACH $key => $value de ' . $total . ' elementos', 'aso3', 'aso4', 1/10 );
	
	Benchmark::setMark( 'aso5' );
	$keys = array_keys( $asoc );
	foreach( $keys as $key )
		if( $asoc[$key] )
			;
	Benchmark::setMark( 'aso6' );
	show( 'Recorriendo ASOC con FOREACH array_keys ( $keys ) as $key de ' . $total . ' elementos', 'aso5', 'aso6', 2/3 );
	
	Benchmark::setMark( 'aso7' );
	$keys = array_keys( $asoc );
	for( $c = 0, $max = count( $keys ); $c < $max; $c ++ )
		if( $asoc[$keys[$c]] )
			;
	Benchmark::setMark( 'aso8' );
	show( 'Recorriendo ASOC con FOR usando array_keys ( $keys ) de ' . $total . ' elementos', 'aso7', 'aso8', 1 );
	
	echo "<h2>Map</h2>";
	
	Benchmark::setMark( 'map1' );
	$map = new \dayscript\Map( );
	foreach( $asoc as $key => $value )
		$map[$key] = $value;
	unset( $map );
	Benchmark::setMark( 'map2' );
	show( 'Llenando mapa con FOREACH de ' . $total . ' elementos', 'map1', 'map2', 1 );
	
	Benchmark::setMark( 'map3' );
	$map = new \dayscript\Map( $asoc );
	unset( $map );
	Benchmark::setMark( 'map4' );
	show( 'Llenando mapa con putAll de ' . $total . ' elementos', 'map3', 'map4', 0 );
?>
<br />
<br />
<pre>
<b>Benchmark:</b><?php echo Benchmark::getInstance( ) ?>
</pre>
</body>
</html>